package com.kuaimacode.kframework.agent.services.master;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaimacode.kframework.mybatis.mapper.master.MasterOrderAcctityMapper;
import com.kuaimacode.kframework.mybatis.models.master.MasterAccitityData;
import com.kuaimacode.kframework.mybatis.models.master.MasterOrderAccitity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 分站信息表
 服务类
 * </p>
 *
 * @author Bean
 * @since 2019-03-13
 */
@Service
public class MasterOrderAccitityService extends ServiceImpl<MasterOrderAcctityMapper, MasterOrderAccitity> {
    


    @Transactional
	public boolean updateOrderAccitity(Long orderId,String status){
	    QueryWrapper<MasterOrderAccitity> accitityQueryWrapper = new QueryWrapper<>();
	    accitityQueryWrapper.eq("order_id",orderId);
//	    accitityQueryWrapper.eq("status",status);
	    MasterOrderAccitity accitity = baseMapper.selectOne(accitityQueryWrapper);
	    accitity.setStatus(status);
	    return baseMapper.updateById(accitity)>0;
    }


    public MasterAccitityData getMasterAccitityData(Long orderId,String orderNo){
	    return baseMapper.selectAccitity(orderNo,orderId);
    }

    @Transactional
    public boolean updateAcctityById(MasterOrderAccitity accitity){
	    return baseMapper.updateById(accitity)>0;
    }

    @Transactional
    public boolean saveAcctity(MasterOrderAccitity accitity){
        QueryWrapper<MasterOrderAccitity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",accitity.getOrderId());
        queryWrapper.eq("status","1");
        List<MasterOrderAccitity> masterOrderAccitities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(masterOrderAccitities)){
            return true;
        }
	    return baseMapper.insert(accitity)>0;
    }

    public MasterOrderAccitity getMasterAccitityByOrderId(Long orderId,String orderNo){
	    QueryWrapper<MasterOrderAccitity> queryWrapper = new QueryWrapper<>();
	    queryWrapper.eq("order_id",orderId);
	    if (StringUtils.isNotEmpty(orderNo)){
	        queryWrapper.eq("order_no",orderNo);
        }
        return baseMapper.selectOne(queryWrapper);
    }

    public MasterOrderAccitity getMasterAccititysByOrderId(Long orderId,String orderNo){
        QueryWrapper<MasterOrderAccitity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        if (StringUtils.isNotEmpty(orderNo)){
            queryWrapper.eq("order_no",orderNo);
        }
        List<MasterOrderAccitity> list = baseMapper.selectList(queryWrapper);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
}
